package ecom.appcode.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecom.appcode.business.ShoesLogic;
import ecom.appcode.business.ShoesLogicImpl;
import ecom.appcode.common.constants.CommonConstants;
import ecom.appcode.common.daobean.ShoesDaoBean;

/**
 * Servlet implementation class ShoesManagerAction
 */
public class ShoesManagerAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShoesManagerAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter(CommonConstants.PARAM_BUTTON_SUBMIT);
        if(CommonConstants.BUTTON_DELETE.equals(action)){
            String string_shoesID = request.getParameter(CommonConstants.PARAM_SHOES_ID);
            int shoesID = Integer.parseInt(string_shoesID);
            System.out.println(shoesID + " BBBBBBB");
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            boolean result = shoesLogic.deleteShoes(shoesID);
            
            if(result) {
                List<ShoesDaoBean> shoesList = shoesLogic.loadAllShoes();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.LOAD_ALL_SHOES, shoesList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.PRODUCT_MANAGER_PAGE);
                rd.forward(request, response);
            }
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter(CommonConstants.PARAM_BUTTON_SUBMIT);

        if (CommonConstants.VALUE_BUTTON_SUBMIT.equals(action)) {

            String shoesName = request.getParameter(CommonConstants.PARAM_SHOES_NAME);
            String color = request.getParameter(CommonConstants.PARAM_SHOES_COLOR);
            String string_unitPrice = request.getParameter(CommonConstants.PARAM_SHOES_UNIT_PRICE);
            float unitPrice = Float.parseFloat(string_unitPrice);
            String string_quantity = request.getParameter(CommonConstants.PARAM_SHOES_QUANTITY);
            int quantity = Integer.parseInt(string_quantity);
            String sex = request.getParameter(CommonConstants.PARAM_SHOES_SEX);
            // URL IMAGE
            String image = request.getParameter(CommonConstants.PARAM_URL_IMAGE);
            String urlImage = request.getParameter(CommonConstants.PARAM_URL_UPLOAD) + image;
            // DATE CARRY
            String day = request.getParameter(CommonConstants.PARAM_SHOES_DAY);
            String month = request.getParameter(CommonConstants.PARAM_SHOES_MONTH);
            String year = request.getParameter(CommonConstants.PARAM_SHOES_YEAR);
            String dateCarry = month + CommonConstants.SLASH + day + CommonConstants.SLASH + year;

            String string_categoryID = request.getParameter(CommonConstants.PARAM_SHOES_CATEGORY_ID);
            int categoryID = Integer.parseInt(string_categoryID);
            String string_discount = request.getParameter(CommonConstants.PARAM_SHOES_DISCOUNT);
            float discount = Float.parseFloat(string_discount);
            String description = request.getParameter(CommonConstants.PARAM_SHOES_DESCRIPTION);
            String brand = request.getParameter(CommonConstants.PARAM_SHOES_BRAND);

            ShoesLogic shoesLogic = new ShoesLogicImpl();
            boolean result = shoesLogic.addNewShoes(shoesName, color, unitPrice, quantity, urlImage, categoryID, sex,
                    dateCarry, discount, description, brand);
            if(result) {
                List<ShoesDaoBean> shoesList = shoesLogic.loadAllShoes();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.LOAD_ALL_SHOES, shoesList);
                session.setAttribute(CommonConstants.ADD_MESSAGE_SUCCESSFUL, "Product added sucessfully");
                request.setAttribute(CommonConstants.ADD_MESSAGE_SUCCESSFUL, "Product added sucessfully");
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.PRODUCT_MANAGER_PAGE);
                rd.forward(request, response);
            }
           
        } else if (CommonConstants.BUTTON_EDIT.equals(action)) {
            String string_shoesID = request.getParameter(CommonConstants.PARAM_SHOES_ID);
            int shoesID = Integer.parseInt(string_shoesID);
            String shoesName = request.getParameter(CommonConstants.PARAM_SHOES_NAME);
            String color = request.getParameter(CommonConstants.PARAM_SHOES_COLOR);
            String string_unitPrice = request.getParameter(CommonConstants.PARAM_SHOES_UNIT_PRICE);
            float unitPrice = Float.parseFloat(string_unitPrice);
            String string_quantity = request.getParameter(CommonConstants.PARAM_SHOES_QUANTITY);
            int quantity = Integer.parseInt(string_quantity);
            String sex = request.getParameter(CommonConstants.PARAM_SHOES_SEX);
            // URL IMAGE
            String image = request.getParameter(CommonConstants.PARAM_URL_IMAGE);
            String urlImage = request.getParameter(CommonConstants.PARAM_URL_UPLOAD) + image;
            // DATE CARRY
            String day = request.getParameter(CommonConstants.PARAM_SHOES_DAY);
            String month = request.getParameter(CommonConstants.PARAM_SHOES_MONTH);
            String year = request.getParameter(CommonConstants.PARAM_SHOES_YEAR);
            String dateCarry = month + CommonConstants.SLASH + day + CommonConstants.SLASH + year;

            String string_categoryID = request.getParameter(CommonConstants.PARAM_SHOES_CATEGORY_ID);
            int categoryID = Integer.parseInt(string_categoryID);
            String string_discount = request.getParameter(CommonConstants.PARAM_SHOES_DISCOUNT);
            float discount = Float.parseFloat(string_discount);
            String description = request.getParameter(CommonConstants.PARAM_SHOES_DESCRIPTION);
            String brand = request.getParameter(CommonConstants.PARAM_SHOES_BRAND);

            ShoesLogic shoesLogic = new ShoesLogicImpl();
            boolean result = shoesLogic.updateShoes(shoesID, shoesName, color, unitPrice, quantity, urlImage, categoryID, sex, dateCarry, discount, description, brand);
            if(result) {
                List<ShoesDaoBean> shoesList = shoesLogic.loadAllShoes();
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.LOAD_ALL_SHOES, shoesList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.PRODUCT_MANAGER_PAGE);
                rd.forward(request, response);
            }
           
        } 
    }
}
